<?php

/**
* This view trait converts the internal data representation into HTML
*/ 
class Nop_Html implements Nop_View_Trait {
    public function View($id) {
        if ($id) $data = $this->Read($id);
        else return;

        $ret = "<div style='width:900px'>";
        foreach($this->GetProperties() as $property_name => $val) {
            $access = $this->GetPropertyAnnotation($property_name,'Access');
            if (FALSE !== $access) {
                $access = $access->value;
                if ((strpos($access,'R') !== FALSE)) $is_readable = true;
                else $is_readable = false;
            }
            else $is_readable = true;
            
            if (FALSE == $this->GetPropertyAnnotation($property_name,'Hidden') AND $is_readable) {
                $property_display = preg_replace("([A-Z])",' $0', substr($property_name,1));
                $ret .=  "<div style=\"\"><label for=\"$property_name\">" . $property_display . ":</label> <span>" . @$data[substr($property_name,1)] . "</span></div>";

            }
        }
        $ret .= "</div>";
        
        return $ret;
    }

    public function Edit($id) {
        if ($id) $data = $this->Read($id);
        else $data = array();
        
        $prefix = "";
        $ret = "<form action=\"/save\">";
        $ret .= "<div style='width:900px'>";
        foreach($this->GetProperties() as $property_name => $val) {
            $access = $this->GetPropertyAnnotation($property_name,'Access');
            if (FALSE !== $access) {
                $access = $access->value;
                if ((strpos($access,'C') !== FALSE AND !$id) OR 
                    (strpos($access,'U') !== FALSE AND $id)) $is_writable = true;
                else $is_writable = false;
            }
            else $is_writable = true;
            
            if (FALSE == $this->GetPropertyAnnotation($property_name,'Hidden') AND $is_writable AND FALSE == $this->GetPropertyAnnotation($property_name,'PrimaryKey')) {
                $type = $this->GetPropertyAnnotation($property_name,"Type");
                if (FALSE == $type) $type = "string";
                else $type = $type->value;

                $property_display = preg_replace("([A-Z])",' $0', substr($property_name,1));
                $value = (array_key_exists(substr($property_name,1),$data)) ? $data[substr($property_name,1)]:"";
                $ret .= "<div style=\"\"><label for=\"$property_name\">" . $property_display . ":</label>";
                
                switch($type) {
                    default:
                    case "string":
                        $ret .=  "<input type=\"text\" id=\"" . $property_name . "_$prefix\" name=\"$property_name\" value=\"" . $value . "\"></div>";
                        break;
                    case "enum":
                        $method = $property_name . "Choices";
                        $choices = $this->$method($id);
                        
                        $ret .= "<select>";
                        foreach($choices as $choice) {
                            if(is_array($choice)) 
                            $ret .= "<option name=" . $choice['Id'] . ">" . $choice['Name'] . "</option>";
                        }
                        $ret .= "</select></div>";
                }
            }
        }
        $ret .= "</div></form>";
        
        return $ret;
    }

    public function Save($id, $data) {
        if ($id) return $this->Update($id, $data);
        else return $this->Create($data);
    }

    public function Remove($id) {
    }
}
?>